package com.example.yigoumall.service;

import com.example.yigoumall.common.JwtUtil;
import com.example.yigoumall.common.MD5Util;
import com.example.yigoumall.dto.LoginRequest;
import com.example.yigoumall.dto.LoginResponse;
import com.example.yigoumall.dto.RegisterRequest;
import com.example.yigoumall.entity.User;
import com.example.yigoumall.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AuthService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private JwtUtil jwtUtil;
    
    // 用户登录
    public LoginResponse login(LoginRequest loginRequest) {
        // 先根据用户名查找用户
        User user = userMapper.findByLoginName(loginRequest.getLoginName());
        
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        
        // 验证密码（使用MD5加密比较）
        String encryptedPassword = MD5Util.md5(loginRequest.getPassword());
        if (!encryptedPassword.equals(user.getPassword())) {
            throw new RuntimeException("用户名或密码错误");
        }
        
        // 生成token
        String token = jwtUtil.generateToken(
                user.getId(), 
                user.getUserName(), 
                user.getType()
        );
        
        return new LoginResponse(
                user.getId(),
                user.getLoginName(),
                user.getUserName(),
                user.getType(),
                token
        );
    }
    
    // 用户注册
    @Transactional
    public LoginResponse register(RegisterRequest registerRequest) {
        // 检查用户名是否已存在
        User existingUser = userMapper.findByLoginName(registerRequest.getLoginName());
        if (existingUser != null) {
            throw new RuntimeException("用户名已存在");
        }
        
        // 创建新用户
        User newUser = new User();
        newUser.setLoginName(registerRequest.getLoginName());
        newUser.setUserName(registerRequest.getUserName());
        // 使用MD5加密密码
        newUser.setPassword(MD5Util.md5(registerRequest.getPassword()));
        newUser.setSex(registerRequest.getSex());
        newUser.setIdentityCode(registerRequest.getIdentityCode());
        newUser.setEmail(registerRequest.getEmail());
        newUser.setMobile(registerRequest.getMobile());
        newUser.setType(registerRequest.getType() != null ? registerRequest.getType() : 0); // 默认为普通用户
        
        userMapper.insert(newUser);
        
        // 生成token
        String token = jwtUtil.generateToken(
                newUser.getId(), 
                newUser.getUserName(), 
                newUser.getType()
        );
        
        return new LoginResponse(
                newUser.getId(),
                newUser.getLoginName(),
                newUser.getUserName(),
                newUser.getType(),
                token
        );
    }
    
    // 验证token
    public boolean validateToken(String token) {
        return jwtUtil.validateToken(token);
    }
    
    // 获取用户ID从token
    public Integer getUserIdFromToken(String token) {
        return jwtUtil.getUserIdFromToken(token);
    }
    
    // 获取用户类型从token
    public Integer getUserTypeFromToken(String token) {
        return jwtUtil.getUserTypeFromToken(token);
    }
}